<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：忘记密码控制器。
 *
 **/
namespace Api\Controller;

class ForgetPwdController extends BaseController
{
    public function _initialize(){
//         header('Content-Type:application/json; charset=utf-8');
//         header('Access-Control-Allow-Origin:*');
//         header('Access-Control-Allow-Methods:*');
//         header('Access-Control-Allow-Headers:*');
    }
    
    /**
     * 忘记密码
     */
    public function index()
    {
        if(!IS_POST){
            $this->res['code'] = 100;
            $this->res['msg'] = '请用post方法请求接口';
            $this->response($this->res,'json');
        }
        //接收注册表单数据
        $post_data = file_get_contents('php://input');
        $json = json_decode($post_data);
        $mobile = $json->mobile;
        $code = $json->code;
        $passowrd = $json->password;
        $confirm_pwd = $json->confirm_pwd;
        
        if($mobile == '' || $code == '' || $passowrd == '' || $confirm_pwd == ''){
            $this->res['code'] = 101;
            $this->res['msg'] = '手机号、验证码、密码及确认密码存在空项';
            $this->response($this->res,'json');
        }
        
        if($passowrd != $confirm_pwd){
            $this->res['code'] = 102;
            $this->res['msg'] = '两次密码输入不一致';
            $this->response($this->res,'json');
        }
        
        $num = M('user')->where(array('username'=>$mobile))->count();
        if($num == 0){
            $this->res['code'] = 103;
            $this->res['msg'] = '手机号不存在';
            $this->response($this->res,'json');
        }
        
        $save_code = M('sms_code')->where(array('mobile'=>$mobile))->find();
        if($save_code['expire_t'] < time() || $save_code['code'] != $code){
            $this->res['code'] = 104;
            $this->res['msg'] = '验证码错误';
            $this->response($this->res,'json');
        }
        
        $res = M('user')->where(array('username'=>$mobile))->data(array('password'=>md5($passowrd),'update_t'=>time()))->save();
        if($res){
            $this->res['msg'] = '新密码设置成功！';
            $this->response($this->res,'json');
        }else{
            $this->res['code'] = 105;
            $this->res['msg'] = '操作失败！';
            $this->response($this->res,'json');
        }
    }
}